// 代码中会兼容本地 service mock 以及部署站点的静态数据
import { generate as generateId } from 'shortid';
import Mock from 'mockjs';
import authorityInfo from './user_json/authorityInfo.json';
import allPermissions from './user_json/allPermissions.json';

const { Random } = Mock;
function commentFunc(req, res) {
  const commentRes = {
    code: '000000',
    message: '成功',
    data: {},
  };
  res.json(commentRes);
}
Random.extend({
  status() {
    const constellations = ['ENABLED', 'DISABLED'];
    return this.pick(constellations);
  },
  gender() {
    const constellations = ['MALE', 'FEMALE'];
    return this.pick(constellations);
  },
  role() {
    const constellations = ['技术', '运营', '编辑'];
    return this.pick(constellations);
  },
  permission() {
    const constellations = ['1', '2', '3', '4'];
    return this.pick(constellations);
  },
  verificationStatus() {
    const constellations = [1, 2];
    return this.pick(constellations);
  },
});
const loop = (data, key, callback) => {
  data.forEach((item, index, arr) => {
    if (item.id === key) {
      return callback(item, index, arr);
    }
    if (item.children) {
      return loop(item.children, key, callback);
    }
  });
};

export default {
  // 支持值为 Object 和 Array
  // 获取权限类型列表
  'GET /admin/api/queryPermissionsTypeList': (req, res) => {
    res.json({
      code: '000000',
      message: '操作成功',
      data: [
        {
          code: 'menu',
          name: '菜单',
        },
        {
          code: 'page',
          name: '页面',
        },
        {
          code: 'group',
          name: '分组',
        },
        {
          code: 'list',
          name: '列表',
        },
        {
          code: 'interface',
          name: '接口',
        },
        {
          code: 'element',
          name: '元素',
        },
      ],
    });
  },

  // 获取所有的权限树
  'POST /admin/api/queryPermissions': (req, res) => {
    res.json({
      code: '000000',
      message: '操作成功',
      data: allPermissions,
    });
  },
  // 获取权限详情
  'POST /admin/api/queryPermissionsInfo': (req, res) => {
    const { id: $id } = req.body;

    loop(allPermissions, $id, item => {
      if (!item) {
        return res.json({
          code: '000000',
          message: '操作成功',
          data: {},
        });
      }
      const { id, name, type } = item;

      const { data } = Mock.mock({
        // 属性 list 的值是一个数组，其中含有 1 到 10 个元素
        data: {
          id,
          name,
          type,
          parentId: $id,
          status: '@status',
          verificationStatus: '@verificationStatus',
          permission: '@string(6):@string(8)',
          url: 'channelreadbuzz/del/button',
          desces: '@string(16)',
        },
      });

      res.json({
        code: '000000',
        message: '操作成功',
        data,
      });
    });
  },
  'POST /admin/api/addPermission': commentFunc,
  'POST /admin/api/editPermission': commentFunc,
  'POST /admin/api/deletePermission': commentFunc,
};
